package com.hyts.standard.webflux.client;

import lombok.Builder;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

/**
 * project-name:standard-webflux
 * package-name:com.hyts.standard.webflux.client
 * author:Libo/Alex
 * create-date:2019-06-19 10:31
 * email:liboware@gmail.com
 * description:此类主要用于:
 */
@Component
public class HttpRequestClient {


    protected WebClient webClient;


    private String basePath;

    /**
     * 构造器
     * @param basePath
     */
    public HttpRequestClient(String basePath){
        this.basePath = basePath;
        webClient = WebClient.builder().baseUrl(basePath).build();
    }

    /**
     * 构造器
     */
    public HttpRequestClient(){
        this.basePath = basePath;
        webClient = WebClient.builder().build();
    }

    /**
     * 请求操作
     * @param path
     * @return
     */
    public Mono<String> get(String path){
        return webClient.get().uri(path).
                accept(MediaType.TEXT_PLAIN).exchange().
                flatMap(clientResponse -> clientResponse.bodyToMono(String.class));
    }
}
